package com.glory.hikvisionmobile.adapter;

import android.graphics.Color;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.bean.TrackIn.ToArrayBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * FileName: ToArrayListAdapter
 * Author: XJH
 * Date: 2020/5/11 10:53
 * Description:
 * History:
 */
public class ToArrayListAdapter extends BaseQuickAdapter<ToArrayBean.ToArrayList,BaseViewHolder> {

    private TextView btnTrackIn;
    private TextView btnTrackOut;
    private TextView btnAbout;
    private boolean holdState;
    private List<ToArrayBean.ToArrayList> selectorData = new ArrayList<>();
    private Map<String,ToArrayBean.ToArrayList> dataMap = new HashMap<>();

    public ToArrayListAdapter(@Nullable List<ToArrayBean.ToArrayList> data) {
        super(R.layout.adapter_to_array,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ToArrayBean.ToArrayList item) {
        helper.setText(R.id.tvItem1,String.valueOf(helper.getAdapterPosition() + 1));
        helper.setText(R.id.tvItem2,item.getComponentId());
        helper.setText(R.id.tvItem3,item.getRecipeName());
        helper.setText(R.id.tvItem4,item.getOwner());
        helper.setText(R.id.tvItem5,item.getState());
        helper.setGone(R.id.tvItem6,false);
        setBackGround(helper,item.getState());
        CheckBox checkBox = helper.getView(R.id.cbCheck);
        checkBox.setChecked(item.isChecked());
        int position = helper.getAdapterPosition();
        checkBox.setOnClickListener(view -> {

            if (checkBox.isChecked()){
                if (dataMap.size() > 0){
                    for(Map.Entry<String,ToArrayBean.ToArrayList> entry : dataMap.entrySet()){
                        if (!entry.getValue().getState().equals(item.getState())){
                            Toast.makeText(mContext, "请选择相同状态的片号操作", Toast.LENGTH_SHORT).show();
                            item.setChecked(false);
                            notifyDataSetChanged();
                            return;
                        }
                    }
                }
                item.setChecked(true);
                dataMap.put(String.valueOf(position),item);
                setBtnState(item);
            }else {
                if (dataMap.containsKey(String.valueOf(position))){
                    item.setChecked(false);
                    dataMap.remove(String.valueOf(position));
                    if (dataMap.size() <= 0){
                        setBottomEnable();
                    }
                }
            }
//            selectorData.clear();
//            if (selectorPos == -1){
//                item.setChecked(true);
//                selectorPos = position;
//                setBtnState(item);
//            }else if (selectorPos != position){
//                getData().get(selectorPos).setChecked(false);
//                item.setChecked(true);
//                selectorPos = position;
//                setBtnState(item);
//            }
            notifyDataSetChanged();
        });
    }

    private void setBackGround(BaseViewHolder helper, String state) {
        switch (state){
            case "RUN":
                helper.setTextColor(R.id.tvItem1, Color.GREEN);
                helper.setTextColor(R.id.tvItem2, Color.GREEN);
                helper.setTextColor(R.id.tvItem3, Color.GREEN);
                helper.setTextColor(R.id.tvItem4, Color.GREEN);
                helper.setTextColor(R.id.tvItem5, Color.GREEN);
                break;
            case "FIN":
                helper.setTextColor(R.id.tvItem1, Color.YELLOW);
                helper.setTextColor(R.id.tvItem2, Color.YELLOW);
                helper.setTextColor(R.id.tvItem3, Color.YELLOW);
                helper.setTextColor(R.id.tvItem4, Color.YELLOW);
                helper.setTextColor(R.id.tvItem5, Color.YELLOW);
                break;
            case "WAIT":
                helper.setTextColor(R.id.tvItem1, Color.BLACK);
                helper.setTextColor(R.id.tvItem2, Color.BLACK);
                helper.setTextColor(R.id.tvItem3, Color.BLACK);
                helper.setTextColor(R.id.tvItem4, Color.BLACK);
                helper.setTextColor(R.id.tvItem5, Color.BLACK);
                break;
        }
    }

    private void setBtnState(ToArrayBean.ToArrayList item) {
        if (holdState){
            return;
        }
        switch (item.getState()){
            case "WAIT":
                btnTrackIn.setEnabled(true);
                btnTrackIn.setBackground(mContext.getResources().getDrawable(R.drawable.free_ui_glory_selector_click_no_corner_background));
                btnTrackOut.setEnabled(false);
                btnAbout.setEnabled(false);
                btnTrackOut.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
                btnAbout.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
                break;
            case "RUN":
                btnTrackOut.setEnabled(true);
                btnTrackOut.setBackground(mContext.getResources().getDrawable(R.drawable.free_ui_glory_selector_click_no_corner_background));
                btnAbout.setEnabled(true);
                btnTrackIn.setEnabled(false);
                btnTrackIn.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
                btnAbout.setBackground(mContext.getResources().getDrawable(R.drawable.free_ui_glory_selector_click_no_corner_background));
                break;
            case "FIN":
                btnTrackOut.setEnabled(false);
                btnAbout.setEnabled(false);
                btnTrackIn.setEnabled(false);
                btnTrackOut.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
                btnAbout.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
                btnTrackIn.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
        }

    }

    private void setBottomEnable() {
        btnTrackIn.setEnabled(false);
        btnTrackOut.setEnabled(false);
        btnAbout.setEnabled(false);
        btnAbout.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
        btnTrackOut.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
        btnTrackIn.setBackgroundColor(mContext.getResources().getColor(R.color.free_ui_blue_sky_text_gray));
    }

    public void setButton(TextView btnTrackIn, TextView btnTrackOut, TextView btnAbout, boolean holdState){
        this.btnTrackIn = btnTrackIn;
        this.btnTrackOut = btnTrackOut;
        this.btnAbout = btnAbout;
        this.holdState = holdState;
    }

    public List<ToArrayBean.ToArrayList> getSelectorData(){
        this.selectorData.clear();
        for(Map.Entry<String,ToArrayBean.ToArrayList> entry : dataMap.entrySet()){
                this.selectorData.add(entry.getValue());

        }
        return this.selectorData;
    }

    public void clear(){
        this.selectorData.clear();
        this.dataMap.clear();
    }
}
